package com.eCommerce.server.sisv.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.eCommerce.corecontract.criteria.UserCriteria;
import com.eCommerce.corecontract.dto.UserDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommerceSISVException;
import com.eCommerce.server.corebackend.dao.DAOController;
import com.eCommerce.server.corebackend.dao.ICategorieDAO;
import com.eCommerce.server.corebackend.dao.IComposantDAO;
import com.eCommerce.server.corebackend.dao.IUserDAO;
import com.eCommerce.server.corebackend.sisv.IUserSISV;
import com.eCommerce.server.corebackend.util.BackEndConstants;

public class UserSISVImpl extends AbstractCommonSISV implements IUserSISV {
	
	private static Logger log =Logger.getLogger(UserSISVImpl.class);
	private DAOController _daoController = null;
	private IUserDAO _userDAOImpl = null;
	
	public UserSISVImpl(DAOController daoController) throws Exception
	{
		_daoController = daoController;
		_userDAOImpl = (IUserDAO) _daoController.get(BackEndConstants.DAO_USER);
	}

	public Long creerUser(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return _userDAOImpl.insert(userDTO);
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		
		catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-001", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public List listerUsers()
			throws EcommerceException {
		try {
			return _userDAOImpl.findAll();//(userCriteria);//insert(userDTO);
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		
		catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public void modifierUser(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			_userDAOImpl.update(userDTO);//(userDTO);
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		
		catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-002", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}

	public List rechercherUsers(UserCriteria userCriteria)
			throws EcommerceException {
		try {
			return _userDAOImpl.find(userCriteria);//insert(userDTO);
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		
		catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public UserDTO seConnecter(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		UserDTO userConnected=null;
	try
	{
		boolean m_Connect= _userDAOImpl.checkLogin(userDTO.getLogin(), userDTO.getPassword());
		
		if (m_Connect){
			String login=userDTO.getLogin();
			userConnected=_userDAOImpl.getUser(login);
		}
		else 
		{
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-006", arg);
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	} 
	catch (EcommerceException ex)
	{
		throw ex;
	}
	catch (Exception ex)
	{
		Object[] arg = new Object[1];
		EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-007", arg,ex.getMessage());
		log.error(sisvException.getMessage());
		throw sisvException;
	}
		
		return userConnected;
	}

	public void seDeconnecter() throws EcommerceException {
		// TODO Auto-generated method stub
		
	}

	public void supprimer(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			_userDAOImpl.delete(userDTO);
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		
		catch (Exception e) {
			Object[] arg = new Object[1];
			arg[0]=userDTO.getNom();
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-005", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public boolean validerModifUser(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean validerUser(UserDTO userDTO, String mode) throws EcommerceException {
		// TODO Auto-generated method stub
		
		try 
		{
			
				
				if (mode.equals("CREATE"))
				{
					// recherche pasr le login du User
					UserDTO userDTO1 = _userDAOImpl.getUser(userDTO.getLogin());
					if (userDTO1!=null) return false;
					else
					{
						// recherche suivant l'email du User
						
						UserDTO userdTO2= _userDAOImpl.getUserByEmail(userDTO.getEmail());
										
						if (userdTO2!=null) return false; 
						
					}
						
				}
		}
		
		catch(EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			arg[0]=userDTO.getNom();
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-008", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
		return true;
	}

	public UserDTO visualiserUser(UserDTO userDTO) throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return _userDAOImpl.findOne(userDTO);//(userCriteria);//insert(userDTO);
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		
		catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVUSER-003", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

}
